# -*- coding;utf-8 -*-
"""
File name : signals.PY
Program IDE : PyCharm
Create file time: 2023/10/21 8:32
File Create By Author : 朱清闯
"""

from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from question.models import DownloadQuestionLog, DownloadGuidePaperLog, UserDoQuestionLog


# 当DownloadQuestionLog模型发生创建操作时，触发相应题目下载量的变化
@receiver(post_save, sender=DownloadQuestionLog)
def create_download_question_log_change(sender, instance, created, **kwargs):
    if created:
        question = instance.question
        question.download_total = question.DownloadQuestionLog.count()
        question.save()


# 当DownloadQuestionLog模型发生删除操作时，触发相应题目下载量的变化
@receiver(post_delete, sender=DownloadQuestionLog)
def delete_download_question_log_change(sender, instance, **kwargs):
    question = instance.question
    question.download_total = question.DownloadQuestionLog.count()
    question.save()


# 当DownloadGuidePaperLog模型发生创建操作时，触发相应导学案（课件）变化
@receiver(post_save, sender=DownloadGuidePaperLog)
def create_guide_paper_download_log_change(sender, instance, created, **kwargs):
    if created:
        guide_paper = instance.guide_paper
        guide_paper.download_total = guide_paper.DownloadGuidePaperLog.count()
        guide_paper.save()


# 当DownloadGuidePaperLog模型发生删除操作时，触发相应导学案(课件)下载量的变化
@receiver(post_delete, sender=DownloadQuestionLog)
def delete_download_question_log_change(sender, instance, **kwargs):
    guide_paper = instance.guide_paper
    guide_paper.download_total = guide_paper.DownloadGuidePaperLog.count()
    guide_paper.save()





